package guiElements;

import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JApplet;
import javax.swing.JPanel;


public class MDCFabric extends JApplet {

	private ArrayList<Fabric> fabricData;
	private CardLayout cardLayout;
	private FabricListener fL;
	private JPanel managerPanel;
	private FabricSearchPanelMain searchPanel;
	private FabricResultsPanelMain resultsPanel;
	private Frame loadingPane;
	
	
	public MDCFabric() throws IOException, URISyntaxException {
		fL = new FabricListener();
		uploadCSV();
		
				
		cardLayout = new CardLayout();
		managerPanel = new JPanel(cardLayout);
		managerPanel.setPreferredSize(new Dimension(550,600));
				
		searchPanel = new FabricSearchPanelMain();
		searchPanel.getSubmitButton().addActionListener(fL);
		searchPanel.getResetButton().addActionListener(fL);
		managerPanel.add(searchPanel,"search");
		
		resultsPanel = new FabricResultsPanelMain(fabricData);
		resultsPanel.getResultsSortByPanel().getNameButton().addActionListener(fL);
		resultsPanel.getResultsSortByPanel().getColorButton().addActionListener(fL);
		resultsPanel.getResultsSortByPanel().getPatternButton().addActionListener(fL);
		resultsPanel.getResultsCriteriaPanel().getReturnButton().addActionListener(fL);
		managerPanel.add(resultsPanel,"results");
		
		add(managerPanel);
	}
	
	@Override
	public void init(){
		setPreferredSize(new Dimension(550,600));
		setSize(new Dimension(550,600));
	}

	private void uploadCSV() throws IOException, URISyntaxException {
		
		URL fabricDataURL = getClass().getResource("fabricData.csv");
        BufferedReader CSVFile = new BufferedReader(new InputStreamReader( fabricDataURL.openStream()));
        Scanner scan = new Scanner(CSVFile);
        scan.nextLine();                
		
        
		fabricData = new ArrayList<Fabric>(0);
		while(scan.hasNextLine()){
			Fabric tempFabric = new Fabric(scan.nextLine(),this);
			if(!tempFabric.getStatus().equalsIgnoreCase("Discontinued")){
				fabricData.add(tempFabric);
			}
		}
		System.out.println("finished");
		scan.close();
		
	}
	
	private class FabricListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==searchPanel.getSubmitButton()){
				setCursor(new Cursor(Cursor.WAIT_CURSOR));
				try {
					resultsPanel.updateAfterSearch(searchPanel.fabricSearch(fabricData));
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				resultsPanel.getResultsCriteriaPanel().updateCriteria(searchPanel.getNameSearchPanel().getNameTextField().getText(), 
						searchPanel.getColorSelectorPanel().getSelectedColors(), searchPanel.getPatternSelectorPanel().getSelectedPatterns());
				setCursor(Cursor.getDefaultCursor());
				if(resultsPanel.getFabricResultsPanel().getFabricData().size()>0){
					cardLayout.show(managerPanel, "results");
				}
				System.out.println("submit");
			}
			if(e.getSource()==searchPanel.getResetButton()){
				searchPanel.fabricReset();
				System.out.println("reset");
			}
			if(e.getSource()==resultsPanel.getResultsSortByPanel().getNameButton()){
				resultsPanel.getResultsSortByPanel().getNameButton().setSelected(true);
				resultsPanel.getResultsSortByPanel().getColorButton().setSelected(false);
				resultsPanel.getResultsSortByPanel().getPatternButton().setSelected(false);
				try {
					resultsPanel.updateAfterSort(searchPanel.fabricSearch(fabricData), "fabricName");
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("name");				
			}
			if(e.getSource()==resultsPanel.getResultsSortByPanel().getColorButton()){
				resultsPanel.getResultsSortByPanel().getNameButton().setSelected(false);
				resultsPanel.getResultsSortByPanel().getColorButton().setSelected(true);
				resultsPanel.getResultsSortByPanel().getPatternButton().setSelected(false);
				try {
					resultsPanel.updateAfterSort(searchPanel.fabricSearch(fabricData), "fabricColor");
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("color");
			}
			if(e.getSource()==resultsPanel.getResultsSortByPanel().getPatternButton()){
				resultsPanel.getResultsSortByPanel().getNameButton().setSelected(false);
				resultsPanel.getResultsSortByPanel().getColorButton().setSelected(false);
				resultsPanel.getResultsSortByPanel().getPatternButton().setSelected(true);
				try {
					resultsPanel.updateAfterSort(searchPanel.fabricSearch(fabricData), "fabricPattern");
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("pattern");
			}
			if(e.getSource()==resultsPanel.getResultsCriteriaPanel().getReturnButton()){
				cardLayout.show(managerPanel, "search");
			}
		}
		
	}
}
